package com.kbcss.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * Servlet implementation class Login
 */
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");
		PrintWriter out=response.getWriter();
		RequestDispatcher rd;
		HttpSession ss=request.getSession();
        HttpSession ss1=request.getSession();
		String uid=request.getParameter("uid");
		String pass=request.getParameter("pass");
		String role=null;
        String st="students",fc="faculty",ie="ie";
		try{
		     Class.forName("com.mysql.jdbc.Driver");
		     Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/kbcssdb", "root", "mysql");
		     PreparedStatement ps=con.prepareStatement("select * from userdetails where userid=? and password=?");
		     ps.setString(1,uid);
		     ps.setString(2,pass);
		     ResultSet rs=ps.executeQuery();
		     while(rs.next()){
		    	 role=rs.getString(11);
		     }
		     if(role.equals(st)){
                 System.out.println("role== "+role);
                 ss.setAttribute("id",uid);
                 ss1.setAttribute("home","sprofile.jsp");
                 rd=request.getRequestDispatcher("sprofile.jsp");
                 rd.forward(request,response);
             }
           if(role.equals(fc)){
        	     System.out.println("role== "+role);
                 ss.setAttribute("id", uid);
                 ss1.setAttribute("home","fprofile.jsp");
                 rd=request.getRequestDispatcher("fprofile.jsp");
                 rd.forward(request,response);
             }
 		 if(role.equals(ie)){
                 ss.setAttribute("id", uid);
                 ss1.setAttribute("home","iprofile.jsp");
                 rd=request.getRequestDispatcher("iprofile.jsp");
                 rd.forward(request,response);
             }
		     
		}
		catch(Exception ex){
			out.println("error...."+ex.getMessage());
		}
	}

}
